In a storage system, data consistency is usually retained in a write-ahead logging (WAL) manner. According to this manner, all data to be written to the storage system is first written to a log file of an external storage device (for example, a magnetic disk), and previous data is subsequently updated according to the log file. When a fault such as a power failure or a breakdown occurs in the system, data can be restored according to a log, thereby ensuring data consistency. With development of a next-generation non-volatile memory (NVM) technology, the next-generation NVM has relatively high reading and writing speeds and is byte addressable, and therefore can be used as a system memory. Such a storage class memory (SCM) using the NVM as a medium features non-volatility, and provides a new method for data consistency protection in the storage system.
In a method for implementing data consistency based on an SCM in the prior art, a cache and a log share storage space of the SCM. In this implementation, an SCM block may be used as both a cache block and a log block. In the SCM, a block is used as a basic unit of the storage space. Generally, a size of a block may be 4K bytes. Each block has three state pairs: frozen/normal, dirty/clean, and up-to-date/out-of-date. “frozen” is used to indicate that the block is a log block, that is, data in the block may be used as a log. “normal” is used to indicate that the block is a cache block, that is, the block is used as a cache. “dirty” is used to indicate that data stored in the block has been modified. “clean” is used to indicate that data stored in the block has not been modified. “up-to-date” is used to indicate that data stored in the block is a latest version. “out-of-date” is used to indicate that data stored in the block is an earlier version. In a data update process, first, a block is allocated to data in the memory, and a state of the block is recorded as (normal, clean, up-to-date). After the data is written to the block, the state of the block is updated to (normal, dirty, up-to-date). The block in the (normal, dirty, up-to-date) state can be directly read or written, that is, data can be directly read from the block in the (normal, dirty, up-to-date) state or data can be directly written to the block in the (normal, dirty, up-to-date) state. After the current write operation is completed, during transaction submission, the memory block is used as a log block, and the state of the memory block is modified to (frozen, dirty, up-to-date). When new data is subsequently written to the memory block, the state of the memory block is modified to (frozen, dirty, out-of-date). The memory block in the (frozen, dirty, out-of-date) state may be written back to a magnetic disk. After the memory block is written back to the magnetic disk, the memory block becomes a free block and can be used for a new write operation. Compared with the WAL manner, the method of using the SCM as both cache space and log space reduces data write operations. However, states of blocks need to be maintained, leading to relatively high system overheads. In addition, in this manner, data needs to be updated at a granularity of a block, and when data to be updated is smaller than a block, a write amplification problem is caused. Consequently, data actually written to the magnetic disk is more than data needing to be written to the magnetic disk.